package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 工作台今日数据预览
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = new HashMap<>();
        map.put("begin", beginTime);
        map.put("end", endTime);

        // 总订单
        Integer totalOrderCount = orderMapper.countByMap(map);


        map.put("status", Orders.COMPLETED);

        // 营业额 sum(amount) status = 5
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        // 有效订单 status = 5
        Integer validOrdersCount = orderMapper.countByMap(map);

        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;

        if (totalOrderCount != 0 && validOrdersCount != 0) {
            // 订单完成率=有效订单/总订单
            orderCompletionRate = validOrdersCount.doubleValue() / totalOrderCount;

            // 平均客单价=营业额/有效订单
            unitPrice = turnover / validOrdersCount;
        }

        // 新增用户
        Integer newUsersCount = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrdersCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsersCount)
                .build();
    }

    /**
     * 工作台订单数据预览
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = new HashMap<>();
        map.put("begin", beginTime);
        map.put("end", endTime);

        // 全部订单
        Integer allOrders = orderMapper.countByMap(map);

        // 待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        // 待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        // 已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        // 已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .build();
    }

    /**
     * 工作台菜品数据预览
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap<>();

        // 已启售
        map.put("status", StatusConstant.ENABLE);
        Integer soldCount = dishMapper.countByMap(map);

        // 已停售
        map.put("status", StatusConstant.DISABLE);
        Integer discontinuedCount = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(soldCount)
                .discontinued(discontinuedCount)
                .build();
    }

    /**
     * 工作台套餐数据预览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap<>();

        // 已启售
        map.put("status", StatusConstant.ENABLE);
        Integer soldCount = setmealMapper.countByMap(map);

        // 已停售
        map.put("status", StatusConstant.DISABLE);
        Integer discontinuedCount = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(soldCount)
                .discontinued(discontinuedCount)
                .build();
    }
}
